

import time
from concurrent import futures
import grpc
from proto import msg_pb2,msg_pb2_grpc



_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class TestService(msg_pb2_grpc.GrpcServiceServicer):
    '''
    继承GrpcServiceServicer,实现hello方法
    '''
    def __init__(self):
        pass

    def hello(self, request, context):
        '''
        具体实现hello的方法，并按照pb的返回对象构造HelloResponse返回
        :param request:
        :param context:
        :return:
        '''

        result=f'this is gprc test service,and the request parameters we received are :({request.data},{request.skill.name}) . so {request.data} is an {request.skill.name}'
        list_result = {"12": 1232}

        print(f'服务端收到请求：{request.data,request.skill.name}')

        return msg_pb2.HelloResponse(result2=str(result),map_result=list_result)



def run():
    """
    模拟服务启动
    :return:
    """
    # 启动 rpc 服务，这里可定义最大接收和发送大小(单位M)，默认只有4M
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),options=[
        ('grpc.max_send_message_length', 100 * 1024 * 1024),
        ('grpc.max_receive_message_length', 100 * 1024 * 1024)])
    msg_pb2_grpc.add_GrpcServiceServicer_to_server(TestService(),server)
    server.add_insecure_port('[::]:50052')
    server.start()
    print("start service...")
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        print("stop service...")
        server.stop(0)


if __name__ == '__main__':
    run()
